import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import Users from '@/components/Users'
import Rights from '@/components/Rights'
import Roles from '@/components/Roles'
import Cate from '@/components/Cate'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect:'/welcome',
      children:[
        {
        path:'/welcome',
        component:Welcome
      },
      {
       path:'/users',
       component:Users
      },
      {
        path:'/rights',
        component:Rights
      },
      {
        path:'/roles',
        component:Roles
      },
      {
        path:'/categories',
        component:Cate
      }
    ]
    }
  ]
})
// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  // to要访问的路径
  // from从哪里跳转过来的路径
  // next要跳转到的路径
  if(to.path==="/login") return next()
  const tokenStr = window.sessionStorage.getItem("token")
  if(!tokenStr) return next("/login")
  next()
})
export default router

